<%--
    Document   : update
    Created on : Jul 1, 2012, 12:30:17 AM
    Author     : aspr0
--%>



<%@page import="is.model.dao.EmployeeDao"%>
<%@page import="is.model.dao.DepartmentDao"%>
<%@page import="is.interfaces.Dao"%>
<%@page import="is.web.Rtable"%>
<%@page import="is.model.exceptions.ObjectNotFoundException"%>
<%@page import="is.web.Message"%>
<%@page import="java.lang.NumberFormatException"%>
<%@page import="is.web.GetParam"%>
<%@page import="is.web.Form"%>
<%@page import="is.web.SelectOptions"%>
<%@page import="java.util.List"%>
<%@page import="is.model.bean.Region"%>
<%@page import="is.model.bean.Employee"%>
<%@page import="java.sql.SQLException"%>
<%@page import="is.model.bean.Department"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="is.model.DataSource"%>
<%@page import="is.model.bean.Department"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    request.setCharacterEncoding(is.Conf.ENCODING); // !!!

    Integer id = null;
    String datatype = "";
    String beanId = "";
    String beanName = "";
    String content = "";

    try
    {
        id = Integer.parseInt(request.getParameter("id"));
    }
    catch ( NumberFormatException e )
    {
        /* return to index on wrong id */
        response.sendRedirect(".");
        return;
    }

    try
    {
        DataSource.establishConnection();

        Department initialDepartmentBean = new Department();
        Employee initialEmployeeBean = new Employee();
        Region initialRegionBean = new Region();

        Dao<Department> departmentDao = new DepartmentDao();
        Dao<Employee> employeeDao = new EmployeeDao();

        departmentDao.setInitialBean(initialDepartmentBean);
        employeeDao.setInitialBean(initialEmployeeBean);

        Department department = null;
        Employee employee = null;

        /* Retrieving employee and linked objects */

        initialEmployeeBean.setId(id);
        employeeDao.objectExists();
        employee = employeeDao.retrieve();

        initialDepartmentBean.setId(employee.getDepartmentId());
        department = departmentDao.retrieve();

        LinkedList<String> linkedValues = new LinkedList<String>();
        LinkedList<String> linkTargets = new LinkedList<String>();

        linkedValues.add(department.getName());
        linkTargets.add("../department/");

        String table = Rtable.generate(employee, linkTargets, linkedValues, 3);

        datatype = employee.getTableName();
        beanName = employee.getFirstName()+" "+employee.getLastName();
        beanId = employee.getId().toString();
        content = table;

        DataSource.closeConnection();
    }
    catch (SQLException e)
    {
        e.printStackTrace();
        response.sendRedirect("?"+GetParam.ERROR+"=0");
        return;
    }
    catch(ObjectNotFoundException e)
    {
        response.sendRedirect(".");
        return;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        response.sendRedirect("?"+GetParam.ERROR+"=1");
        return;
    }
%>

<jsp:include page="../template/retrieve.jsp">
    <jsp:param name="datatype" value="<%=datatype%>"/>
    <jsp:param name="beanId" value="<%=beanId%>"/>
    <jsp:param name="beanName" value="<%=beanName%>"/>
    <jsp:param name="content" value="<%=content%>"/>
</jsp:include>